package zhoufu.sz.sanxiao_check.navigation;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.widget.ImageView;
import cn.finalteam.galleryfinal.FunctionConfig;
import cn.finalteam.galleryfinal.GalleryFinal;
import cn.finalteam.galleryfinal.PhotoPreviewActivity;
import cn.finalteam.galleryfinal.model.PhotoInfo;
import com.bumptech.glide.load.resource.bitmap.GlideBitmapDrawable;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.inject.Inject;
import zhoufu.sz.sanxiao_check.MainActivity;
import zhoufu.sz.sanxiao_check.config.Constant;
import zhoufu.sz.sanxiao_check.models.CheckItemModel;
import zhoufu.sz.sanxiao_check.models.StreetInfoModel;
import zhoufu.sz.sanxiao_check.models.StreetListModel;
import zhoufu.sz.sanxiao_check.models.TrainListModel;
import zhoufu.sz.sanxiao_check.ui.CaptureActivity;
import zhoufu.sz.sanxiao_check.ui.FireSafeActivity;
import zhoufu.sz.sanxiao_check.ui.GestureViewActivity;
import zhoufu.sz.sanxiao_check.ui.MainCenterActivity;
import zhoufu.sz.sanxiao_check.ui.SearchActivity;
import zhoufu.sz.sanxiao_check.ui.ShowPicActivity;
import zhoufu.sz.sanxiao_check.ui.StreetDetailsActivity;
import zhoufu.sz.sanxiao_check.ui.check.PrintActivity;
import zhoufu.sz.sanxiao_check.ui.train.AddTrainActivity;
import zhoufu.sz.sanxiao_check.ui.train.TrainDetailsActivity;

/**
 * Created by hyc on 16/5/5.
 * 帮助跳转的 注入类
 */

public class Navigator
{

    @Inject
    public Navigator()
    {
        //empty
    }

    public void startMainAct(Context context)
    {
        Intent intent = new Intent(context, MainActivity.class);
        context.startActivity(intent);
    }

    public void startMainCenterAct(Context context, int Action)
    {
        Intent intent = new Intent(context, MainCenterActivity.class);
        intent.putExtra(Constant.MainType.ACTION_KEY, Action);
        context.startActivity(intent);
    }

    public void startStreetDetailsAct(Context context, StreetListModel model)
    {
        if ( model == null )
        {
            return;
        }
        Intent intent = new Intent(context, StreetDetailsActivity.class);
        intent.putExtra("StreetListModel", model);
        context.startActivity(intent);
    }

    public void startBasicMainAct(Context context, StreetInfoModel model, Class c)
    {
        if ( model == null )
        {
            return;
        }
        Intent intent = new Intent(context, c);
        intent.putExtra("StreetInfoModel", model);
        context.startActivity(intent);
    }

    public void startSearchAct(Context context)
    {
        Intent mIntent = new Intent(context, SearchActivity.class);
        context.startActivity(mIntent);
    }

    public void startScanAct(Context context)
    {
        Intent mIntent = new Intent(context, CaptureActivity.class);
        mIntent.putExtra("zxing_code_id", "1");
        context.startActivity(mIntent);
    }

    public void startAddTrainAct(Context context, String SID)
    {
        Intent mIntent = new Intent(context, AddTrainActivity.class);
        if ( SID == null )
        {
            return;
        }
        mIntent.putExtra("SID", SID);
        context.startActivity(mIntent);
    }

    public void startAddTrainAct(Context context, TrainListModel model)
    {
        if ( model == null )
        {
            return;
        }
        Intent mIntent = new Intent(context, TrainDetailsActivity.class);
        mIntent.putExtra("TrainListModel", model);
        context.startActivity(mIntent);
    }

    //public void selectImage(Fragment fireSafetyFragment, int requestcode)
    //{
    //    Intent intent = new Intent(fireSafetyFragment.getActivity(),
    //            MultiImageSelectorActivity.class);
    //    intent.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA, true);
    //    intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE,
    //            MultiImageSelectorActivity.MODE_SINGLE);
    //    fireSafetyFragment.startActivityForResult(intent, requestcode);
    //}

    public void selectMaxOneImage(GalleryFinal.OnHanlderResultCallback mResultCallback){
        FunctionConfig config = new FunctionConfig.Builder()
                .setEnableCamera(true)
                .setMutiSelectMaxSize(1)
                .build();
        GalleryFinal.openGalleryMuti(1, config, mResultCallback);
    }

    public void showGesture(Fragment fragment, int reqCode)
    {

        fragment.startActivityForResult(
                new Intent(fragment.getActivity(), GestureViewActivity.class),
                reqCode);
    }


    public void startFireSafe(Context context)
    {
        Intent intent = new Intent(context, FireSafeActivity.class);
        context.startActivity(intent);
    }

    public void startPrintAct(Context context, CheckItemModel model) {
        Intent mIntent = new Intent(context, PrintActivity.class);
        if (model == null) {
            return;
        }
        mIntent.putExtra("MODEL", model);
        context.startActivity(mIntent);
    }


    public void startPrintAct(Context context, String SID)
    {
        if ( zhoufu.sz.sanxiao_check.utils.TextUtils.isEmpty(SID) )
        {
            return;
        }
        Intent mIntent = new Intent(context, PrintActivity.class);
        mIntent.putExtra("SID", SID);
        context.startActivity(mIntent);
    }

    public void startShowPic(Context context, String paths)
    {
        if ( zhoufu.sz.sanxiao_check.utils.TextUtils.isEmpty(paths) )
        {
            return;
        }
        Intent          intent   = new Intent(context, PhotoPreviewActivity.class);
        List<String>    list     = Arrays.asList(TextUtils.split(paths, ","));
        List<PhotoInfo> listInfo = new ArrayList<>();
        for (String s : list)
        {
            PhotoInfo info = new PhotoInfo();
            info.setPhotoPath(s);
            listInfo.add(info);
        }
        intent.putExtra("photo_list", (Serializable) listInfo);
        context.startActivity(intent);
    }

    public void goToShowPic(Fragment fragment, ImageView img, int position, ArrayList<String> pics)
    {
        int[] location = new int[2];
        img.getLocationInWindow(location);
        Drawable drawable = img.getDrawable();
        Bitmap   bitmap   = null;
        if ( drawable instanceof GlideBitmapDrawable )
        {
            bitmap = ( (GlideBitmapDrawable) drawable ).getBitmap();
        } else if ( drawable instanceof BitmapDrawable )
        {
            bitmap = ( (BitmapDrawable) drawable ).getBitmap();
        }

        if ( bitmap == null )
        {
            return;
        }

        ActivityOptionsCompat optionsCompat =
                ActivityOptionsCompat.makeThumbnailScaleUpAnimation(img, bitmap, location[0],
                        location[1]);
        Intent intent = new Intent(fragment.getActivity(), ShowPicActivity.class);
        intent.putStringArrayListExtra(ShowPicActivity._PHOTOS, pics);
        intent.putExtra(ShowPicActivity.POSITION, position);
        ActivityCompat.startActivity(fragment.getActivity(), intent, optionsCompat.toBundle());
    }
}
